var constant = require('../common/constant');
homeController.$inject = ['$scope', '$location','homeService'];



function homeController($scope, $location,homeService) {

    homeService.getMerchantMsg(function(resposne){
        if(!resposne.data.result){
            console.log("find merchant fail");
        }else{
            var merchant = resposne.data.obj;
            console.log("account:"+merchant.account);
            $scope._id = merchant._id;
            $scope.imgHead = constant.FileServer+merchant.imgHead;
            $scope.account = merchant.account;
            $scope.name = merchant.name;
            $scope.address = merchant.address;
            $scope.status = merchant.status;
        }
    });

    $scope.getStatus = function(){
        homeService.getStatus(function(response){
            console.log(response);
            if(!response.data.result){
                alert("Request Fail");
            }else{
                var status = response.data.obj;
                if(status==constant.Reject){
                    console.log('reject:'+response.data.errMsg);
                    $scope.rejectContent ="Reason:"+response.data.errMsg;
                }
                $scope.status = response.data.obj;

            }
        });
    };

    $scope.apply = function () {
        var name = $scope.name;
        var address = $scope.address;
        var imgCard = $scope.imgCard;
        var imgHeadApply = $scope.imgHeadApply;
        if(isUndefined(address)||isUndefined(name)){
            $scope.msgNull = true;
        }else{
            var merchant = {};
            merchant.name = name;
            merchant.address = address;
            if(imgCard){
                merchant.imgCard = imgCard;
            }
            if(imgHeadApply){
                merchant.imgHead = imgHeadApply;

            }
            
            homeService.applyStore(merchant,function (response) {
                if(!response.data.result){
                    alert("Request Fail");
                }else{
                    console.log('Request Success');
                    $("#applyModel").modal('hide');
                    $scope.imgHead = constant.FileServer+'head/'+$scope._id+'.jpg?random='+new Date().getTime();
                }
            });


        }
    }
    function isUndefined(obj) {
        if(!obj){
            return true;
        }
        return false;
    }
}

module.exports = homeController;